package com.hy.frame.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.hy.frame.R;
import com.hy.frame.common.ILoadingUI;

/**
 * title LoadingUI
 * author HeYan
 * time 19-7-12 上午11:55
 * desc 无
 */
public class LoadingUI implements ILoadingUI {
    protected Context mContext = null;
    protected View mLayout = null;
    protected View cLoad = null;
    protected boolean replace;
    protected TextView txtMessage = null;
    protected ImageView imgMessage = null;
    protected View vLoading = null;
    protected View.OnClickListener mListener = null;

    /**
     * 初始化
     *
     * @param parent 父布局
     */
    public LoadingUI(@NonNull View parent) {
        this(parent, false);
    }

    /**
     * 初始化
     *
     * @param layout  父布局
     * @param replace 是否替换当前布局
     */
    public LoadingUI(@NonNull View layout, boolean replace) {
        if (cLoad != null) return;
        this.mContext = layout.getContext();
        this.mLayout = layout;
        this.replace = replace;
        //如果没有开启同级模式，则判断传入layout是否是ViewGroup，不是则开启
        if (!replace && !(layout instanceof ViewGroup)) {
            this.replace = true;
        }
        if (!replace) {
            cLoad = layout.findViewById(R.id.kf_load);
        } else {
            cLoad = ((View) layout.getParent()).findViewById(R.id.kf_load);
        }
        //You need to add the layout
        if (cLoad == null) {
            cLoad = View.inflate(layout.getContext(), R.layout.in_loading, null);
            if (replace) {
                ((ViewGroup) layout.getParent()).addView(cLoad, 0);
            } else if (layout instanceof ViewGroup) {
                ((ViewGroup) layout).addView(cLoad, 0);
            }
        }
        this.txtMessage = cLoad.findViewById(R.id.kf_msg);
        this.imgMessage = cLoad.findViewById(R.id.kf_thumb);
        this.vLoading = cLoad.findViewById(R.id.kf_loading);
        hide();
    }

    @Override
    public void show() {
        show(R.string.loading);
    }

    @Override
    public void show(@StringRes int strId) {
        show(this.mContext.getString(strId));
    }

    @Override
    public void show(@Nullable CharSequence msg) {
        if (!isShowing()) {
            showLoading();
        }
        this.imgMessage.setVisibility(View.GONE);
        if (!isLoading()) {
            this.vLoading.setVisibility(View.VISIBLE);
        }
        this.txtMessage.setText(msg);
    }

    @Override
    public void error(@StringRes int strId) {
        error(this.mContext.getString(strId));
    }

    @Override
    public void error(@Nullable CharSequence msg) {
        error(msg, 0);
    }

    @Override
    public void error(@StringRes int strId, @DrawableRes int drawId) {
        error(this.mContext.getString(strId), drawId);
    }

    @Override
    public void error(@Nullable CharSequence msg, @DrawableRes int drawId) {
        if (!isShowing()) {
            show();
        }
        this.imgMessage.setVisibility(View.VISIBLE);
        this.vLoading.setVisibility(View.GONE);
        this.txtMessage.setText(msg);
        if (drawId != 0 && drawId != View.NO_ID)
            this.imgMessage.setImageResource(drawId);
    }

    /**
     * 显示加载布局
     */
    private void showLoading() {
        if (replace) {
            this.mLayout.setVisibility(View.GONE);
        } else if (this.mLayout instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) this.mLayout;
            for (int i = 0; i < parent.getChildCount(); i++) {
                parent.getChildAt(i).setVisibility(View.GONE);
            }
        }
        cLoad.setVisibility(View.VISIBLE);
    }

    @Override
    public void hide() {
        if (replace) {
            this.mLayout.setVisibility(View.VISIBLE);
        } else if (this.mLayout instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) this.mLayout;
            for (int i = 0; i < parent.getChildCount(); i++) {
                parent.getChildAt(i).setVisibility(View.VISIBLE);
            }
        }
        cLoad.setVisibility(View.GONE);
    }

    @Override
    public void setErrorClick(@Nullable View.OnClickListener listener) {
        this.mListener = listener;
        if (cLoad != null)
            cLoad.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imgMessage.getVisibility() == View.VISIBLE) {
                        if (mListener != null)
                            mListener.onClick(v);
                    }
                }
            });
    }

    public boolean isLoading() {
        return vLoading.getVisibility() == View.VISIBLE;
    }

    public boolean isShowing() {
        return cLoad.getVisibility() == View.VISIBLE;
    }
}
